package com.nbsaas.boot.controller;

import com.nbsaas.boot.entity.KnowledgePoint;
import com.nbsaas.boot.service.KnowledgePointService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/knowledge-points")
@RequiredArgsConstructor
public class KnowledgePointController {
    
    private final KnowledgePointService knowledgePointService;

    @PostMapping
    public ResponseEntity<?> createKnowledgePoint(@RequestBody KnowledgePoint knowledgePoint, HttpSession session) {
        try {
            if (session.getAttribute("user") == null) {
                return ResponseEntity.badRequest().body(new ErrorResponse("请先登录"));
            }
            
            KnowledgePoint created = knowledgePointService.createKnowledgePoint(knowledgePoint);
            return ResponseEntity.ok(created);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> updateKnowledgePoint(@PathVariable Long id, @RequestBody KnowledgePoint knowledgePoint, HttpSession session) {
        try {
            if (session.getAttribute("user") == null) {
                return ResponseEntity.badRequest().body(new ErrorResponse("请先登录"));
            }
            
            KnowledgePoint updated = knowledgePointService.updateKnowledgePoint(id, knowledgePoint);
            return ResponseEntity.ok(updated);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteKnowledgePoint(@PathVariable Long id, HttpSession session) {
        try {
            if (session.getAttribute("user") == null) {
                return ResponseEntity.badRequest().body(new ErrorResponse("请先登录"));
            }
            
            knowledgePointService.deleteKnowledgePoint(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getKnowledgePoint(@PathVariable Long id) {
        try {
            KnowledgePoint knowledgePoint = knowledgePointService.getKnowledgePoint(id);
            return ResponseEntity.ok(knowledgePoint);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
        }
    }

    @GetMapping("/search")
    public ResponseEntity<?> searchKnowledgePoints(
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer level,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<KnowledgePoint> knowledgePoints = knowledgePointService.searchKnowledgePoints(
                category, level, keyword, PageRequest.of(page, size));
            return ResponseEntity.ok(knowledgePoints);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
        }
    }

    @Data
    static class ErrorResponse {
        private final String message;
    }
} 